# -*- coding: utf-8 -*-
# @Time    : 2021/10/19 9:18
# @Author  : TangDagui
# @Email   : 3297364807@qq.com
# @File    : 宽带连接.py
# @Software: PyCharm
# @Explain ：请勿乱传哈
import requests
import json
import random
import time


def login(user):
    url = 'http://172.20.253.55/webauth.do?wlanacname=000000021.210.00'
    payload = 'loginType=&auth_type=0&isBindMac1=1&pageid=1&templatetype=1&listbindmac=1&recordmac=0&isRemind=1&loginTimes=&groupId=&distoken=&echostr=&url=&isautoauth=&notice_pic_loop2=%2Fportal%2Fuploads%2Fpc%2Fdemo2%2Fimages%2Fbj.png&notice_pic_loop1=%2Fportal%2Fuploads%2Fpc%2Fdemo2%2Fimages%2Flogo.png&userId={0}&passwd=123&twiceauth=1&remInfo=on'.format(
        user)
    headers = {
        'Content-Type': 'application/x-www-form-urlencoded',
    }
    response = requests.request('POST', url, headers=headers, data=payload)
    pass


def isConnected():
    html = requests.get("http://www.baidu.com", timeout=2)
    if len(html.text) >= 2000 and len(html.text) < 48348:
        return True
    else:
        return False


if __name__ == '__main__':
    count = 0
    while True:
        time.sleep(1)
        if isConnected() == True:
            print("网络正常")
        else:
            count += 1
            print("第{0}次连接".format(count))
            mJson = json.load(open("lib/user.json"))
            mUser = mJson["{}".format(random.randint(0, len(mJson)))]
            login(mUser)
